﻿using ComClassLibrary;
using ComUIControl;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace SoftAllKeyBoard
{
    public partial class FormKeyInputing : Form
    {
        public const int USER_MESSAGE_OK = 0x0405;//用户自定义消息的开始数值
        
        public const int USER_MESSAGE_CLEAR = 0x0406;//用户自定义消息的开始数值
     
        private AllKeyBoard keyboard;

        private bool _isPuttingPwd = true;
        public bool IsPuttingPwd
        {
            set
            {
                _isPuttingPwd = true;
            }
            get
            {
                return _isPuttingPwd;
            }
        }

        public string InputString
        {
            private set;
            get;
        }
        
        private string _title = "";

        private string _psw = "hnymj";
        public string PSWString
        {
            set { _psw = value; }
            get { return _psw; }
        }

        public FormKeyInputing(string title)
        {
            _title = title;
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            InputString = "";
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (Check())
            {
                InputString = textBoxContent.Text.Trim();
                this.DialogResult = DialogResult.OK;
                this.Close();
            }               
        }

        private void FormKeyInputing_Load(object sender, EventArgs e)
        {
            if (_isPuttingPwd)
            {
                textBoxContent.PasswordChar = '*';
            }
            else
            {
                textBoxContent.PasswordChar = (char)0;
            }
            _psw = IniFile.IniValue("Exit", "Psw", "");
            labelTitle.Text = _title;
            keyboard = new AllKeyBoard(this.Handle);
            keyboard.Dock = DockStyle.Fill;
            keyboard.TopLevel = false;
            this.panel1.Controls.Clear();
            this.panel1.Controls.Add(keyboard);
            keyboard.KeyClick += OnKeyClick;
            keyboard.BackspaceKeyClick += OnBackspaceClick;
            keyboard.ButtonOKClick += btnOK_Click;
            keyboard.Show();
            textBoxContent.Focus();
            int scrWidth = Screen.PrimaryScreen.Bounds.Width;
            int scrHeight = Screen.PrimaryScreen.Bounds.Height;
            int x = (scrWidth - this.Width) / 2;
            int y = (scrHeight - this.Height) / 2;
            this.Location = new Point(x, y);
            labelCountdown.Text = CountDownSecond.ToString();
            timer1.Enabled = true;
        }

        /// <summary>
        /// 键盘点击事件
        /// </summary>
        /// <param name="e"></param>
        private void OnKeyClick(object sender, CharClickEventArgs e)
        {
            textBoxContent.AppendText(e.Char);
        }

        /// <summary>
        /// 点击退格事件
        /// </summary>
        /// <param name="e"></param>
        private void OnBackspaceClick(object sender, EventArgs e)
        {
            if (textBoxContent.TextLength > 0)
            {
                textBoxContent.Text = textBoxContent.Text.Substring(0, textBoxContent.TextLength - 1);
            }
        }

        private void OnClear()
        {
            textBoxContent.Clear();
            textBoxContent.Focus();
        }

        public bool Check()
        {        
            if (string.IsNullOrEmpty(textBoxContent.Text))
            {
                MessageBox.Show(this,
                   "输入不能为空!请重新输入...",
                   "错误提示",
                   MessageBoxButtons.OK,
                   MessageBoxIcon.Error);
                return false;
            }
            if (_isPuttingPwd)
            {
                if (textBoxContent.Text == _psw)
                {
                    this.DialogResult = DialogResult.Yes;
                    return true;
                }
                else
                {
                    MsgBox.Show("密码错误");
                    textBoxContent.Focus();
                    //this.DialogResult = DialogResult.No;
                    return false;
                }           
            }
            return true;
        }

        public void OnbuttonOK_Click(ref System.Windows.Forms.Message m)
        {
            if (Check())
            {             
                m.Result = (IntPtr)0x00;
                InputString = textBoxContent.Text.Trim();
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                m.Result = (IntPtr)0xff;               
                textBoxContent.Focus();
            }
        }

        protected override void DefWndProc(ref System.Windows.Forms.Message m)
        {
            switch (m.Msg)
            {
                case USER_MESSAGE_OK:
                    OnbuttonOK_Click(ref m);
                    //base.DefWndProc(ref m);
                    break;
                case USER_MESSAGE_CLEAR:
                    OnClear();
                    break;
                default:
                    base.DefWndProc(ref m);
                    break;
            }
        }

        private void FormKeyInputing_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (keyboard != null && keyboard.IsHandleCreated)
            {
                keyboard.Close();
            }
        }

        private bool IsInMovingArea(Point p)
        {
            if (p.X > labelTitle.Left && p.Y < textBoxContent.Top)
            {
                return true;
            }
            return false;
        }

        private Point _mousePoint;

        private void FormKeyInputing_MouseMove(object sender, MouseEventArgs e)
        {
            if (!IsInMovingArea(e.Location))
            {
                return;
            }
            if (e.Button == MouseButtons.Left)
            {
                int X = MousePosition.X - _mousePoint.X;
                int Y = MousePosition.Y - _mousePoint.Y;
                this.Location = new Point(X, Y);
            }         
        }

        private void FormKeyInputing_MouseDown(object sender, MouseEventArgs e)
        {
            if (!IsInMovingArea(e.Location))
            {
                return;
            }
            if (e.Button == MouseButtons.Left)
            {
                _mousePoint.X = e.X;
                _mousePoint.Y = e.Y;
            }
        }

        private int CountDownSecond = 120;

        private void timer1_Tick(object sender, EventArgs e)
        {
            CountDownSecond--;
            labelCountdown.Text = CountDownSecond.ToString();
            if (CountDownSecond == 0)
            {
                this.Close();
            }
        }
    }
}
